package wnhz.com.business.shareshop.activity.classify;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.WindowManager;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.base.MyBaseActivity;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.netcallback.DialogCallback;
import wnhz.com.business.rebateshop.activity.mine.AllOrderActivity;
import wnhz.com.business.rebateshop.activity.mine.OrderAllFragment;
import wnhz.com.business.rebateshop.activity.mine.OrderFifthFragment;
import wnhz.com.business.rebateshop.activity.mine.OrderFourFragment;
import wnhz.com.business.rebateshop.activity.mine.OrderOneFragment;
import wnhz.com.business.rebateshop.activity.mine.OrderThreeFragment;
import wnhz.com.business.rebateshop.activity.mine.OrderTwoFragment;
import wnhz.com.business.shareshop.activity.classify.fragment.ClassifGoodsFragment;
import wnhz.com.business.shareshop.bean.ClassifyTwoBean;
import wnhz.com.business.shareshop.bean.ShareHomDataBean;
import wnhz.com.business.uitls.LogUtils;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.ToastUtils;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.view.TranslucentActionBar;

/**
 * Created by Administrator on 2017-12-15 0015.
 * 商品种类
 */

public class ClassifGoodsActivity extends BaseActivity implements ActionBarClickListener {
    @BindView(R.id.tl_3)
//    SlidingTabLayout tl_3;
            TabLayout tl_3;
    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;
    @BindView(R.id.vp)
    ViewPager vp;

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private MyPagerAdapter mAdapter;
    private int position;
    private String cate_id;
    private List<ShareHomDataBean.InfoBean.CateListBean> cate_list;
    private List<ClassifyTwoBean.InfoBean> cate_list_two;
    private String title;//二级界面的大标题


    public static Intent createIntent(Context context,int position,String cate_id,String title) {
        Intent intent = new Intent(context, ClassifGoodsActivity.class);
        intent.putExtra("position",position);
        intent.putExtra("cate_id",cate_id);
        intent.putExtra("title",title);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classify_goods);
        ButterKnife.bind(this);
        position = getIntent().getIntExtra("position", 0);
        cate_id = getIntent().getStringExtra("cate_id");
        title = getIntent().getStringExtra("title");
        if (null != title) {
            actionbar.setData(title, R.drawable.ic_lefft_white, null, 0, null, 0, this);
        }else {
            actionbar.setData("手机", R.drawable.ic_lefft_white, null, 0, null, 0, this);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }
       getHomFragmentData();
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {

    }
    public void getHomFragmentData() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getToken());
        map.put("cateId", cate_id);
        map.put("goods_type", "4");


        XUtil.Post(Url.GOODS_GET_ALL_CATE,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    Gson gson = new Gson();

                    if (jsonObject.optString("re").equals("1")) {
                        ClassifyTwoBean classifyTwoBean = gson.fromJson(result,ClassifyTwoBean.class);
//                        cate_list = classifyTwoBean.getInfo();
                        cate_list_two = classifyTwoBean.getInfo();

//                        initType(cate_list);
                        initType(cate_list_two);
                        mAdapter.notifyDataSetChanged();
                    } else if ("-1".equals(jsonObject.optString("re"))){
                        MyToast(getResources().getString(R.string.login_out));
                        MyApplication.getInstance().gotoLoginActivity();
                    }else {
                        MyToast(jsonObject.optString("info"));
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });


    }

    private void initType(List<ClassifyTwoBean.InfoBean> cate_list) {
        if (cate_list_two != null) {
            for (int i = 0; i < cate_list_two.size(); i++) {
                tl_3.addTab(tl_3.newTab().setText(cate_list_two.get(i).getClassname()));
                ClassifGoodsFragment instance = ClassifGoodsFragment.getInstance();
                Bundle args = new Bundle();
                args.putString("cate_id",cate_list_two.get(i).getCate_id());
                args.putString("title",title);
                instance.setArguments(args);
                mFragments.add(instance);
            }
        }
        mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        vp.setAdapter(mAdapter);
        tl_3.setupWithViewPager(vp);
        tl_3.setScrollPosition(position,0,true);
        vp.setCurrentItem(position);
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return cate_list_two.get(position).getClassname();
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
}
